package cn.topevery.garden.dal.logic;

import cn.topevery.garden.dal.dao.FacilitiesClassDao;
import cn.topevery.garden.entity.dto.FacilitiesClassDTO;
import cn.topevery.garden.entity.po.FacilitiesClass;
import cn.topevery.garden.entity.query.FacilitiesClassQuery;
import cn.topevery.garden.entity.vo.FacilitiesClassVO;
import cn.topevery.garden.framework.base.BaseLogic;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.SystemConst;
import hw.topevery.framework.db.base.BaseEntityDao;
import hw.topevery.framework.db.entity.DataPager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author code-generate
 * @date 2021-08-06
 */
@Service
public class FacilitiesClassLogic extends BaseLogic<FacilitiesClass> {
    @Autowired
    FacilitiesClassDao facilitiesClassDao;

    @Autowired
    private Mapper mapper;

    @Override
    public BaseEntityDao<FacilitiesClass, Integer> getDao() {
        return facilitiesClassDao;
    }


    /**
     * 获取列表
     *
     * @param query
     */
    public DataPager<FacilitiesClass> listPage(FacilitiesClassQuery query) {
        return facilitiesClassDao.listPage(query);
    }

    /**
     * 根据自增id批量删除
     *
     * @param ids
     */
    public void deleteBatchById(String userId, Integer[] ids) {
        facilitiesClassDao.delete(userId, ids);
    }

    public List<FacilitiesClassVO> getListByPid(String pid) {
        List<FacilitiesClass> list = facilitiesClassDao.getListByPid(pid);
        return list.stream()
                .map(temp -> mapper.map(temp, FacilitiesClassVO.class)).collect(Collectors.toList());
    }

    public void saveOrUpdate(String userId, FacilitiesClassDTO dto) {
        facilitiesClassDao.saveOrUpdate(userId, mapper.map(dto, FacilitiesClass.class));
    }

    public FacilitiesClassVO getById(Integer id) {
        return mapper.map(facilitiesClassDao.getEntity(id), FacilitiesClassVO.class);
    }

    public void delById(Integer userId, String entityId) {
        facilitiesClassDao.delByEntityId(userId, entityId);
    }

    public List<FacilitiesClassVO> getTree() {
        List<FacilitiesClassVO> list = this.getListByPid(SystemConst.UUID_EMPTY.toString());
        if (list != null && !list.isEmpty()) {
            list = buildTree(list);
        }
        return list;
    }

    private List<FacilitiesClassVO> buildTree(List<FacilitiesClassVO> list) {
        List<FacilitiesClassVO> tree = new ArrayList<>();
        FacilitiesClassVO node;
        List<FacilitiesClassVO> child;
        for (FacilitiesClassVO vo : list) {
            node = mapper.map(vo, FacilitiesClassVO.class);
            child = this.getListByPid(vo.getEntityId());
            if (child != null && !child.isEmpty()) {
                node.setChildren(buildTree(child));
            }
            tree.add(node);
        }
        return tree;
    }
}
